package com.example.websockettest.webSocket.service;

import com.alibaba.fastjson.JSONObject;
import com.example.websockettest.webSocket.entity.*;
import com.example.websockettest.webSocket.mapper.*;
import com.example.websockettest.webSocket.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



/**
 * @author hetong
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService{
    @Autowired
    private GameUserMapper gameUserMapper;
    @Autowired
    private GameUserLoginInfoMapper gameUserLoginInfoMapper;

    public JSONObject handle(String message){
        JSONObject resultData = new JSONObject();
        JSONObject json = JSONObject.parseObject(message);
        String action = json.getString("action");
        if(action!=null){
            if(action.equals("login")){
                //创建用户对象
                GameUser gameUser = new GameUser();
                //给用户对象赋值，账号
                gameUser.setUser(json.getString("user"));
                //通过账号查询该账号对应的用户集合  正常情况只会有一个
                List<GameUser> gameUsers = gameUserMapper.selectGameUserList(gameUser);
                //判断 如果集合大小大于零 则说明账号已存在
                if(gameUsers.size()>0){
                    //账号已存在，走此方法
                    GameUser gameUser1 = gameUsers.get(0);
                    String pwd = json.getString("pwd");
                    Long userId = gameUsers.get(0).getId();
                    //判断密码是否为空
                    if(pwd==null||pwd.equals("")||(pwd.trim()).equals("")){
                        resultData.put("action","login");
                        resultData.put("msg","密码不能为空!");
                        resultData.put("code","-1");
                        resultData.put("uid",userId);
                        return resultData;
                    }

                    //比较密码是否正确
                    if(gameUser1.getPassword().equals(pwd)) {
                        //密码正确 则生成用户token
                        String token = saveToken(String.valueOf(userId));
                        resultData.put("action", "login");
                        resultData.put("msg", "登录成功");
                        resultData.put("code", "1");
                        resultData.put("uid", userId);
                        resultData.put("tk", token);
                        return resultData;
                    }else {
                        //密码错误返回弹出错误
                        resultData.put("action","login");
                        resultData.put("msg","密码错误!!!!!!");
                        resultData.put("code","-1");
                        resultData.put("uid",userId);
                        return resultData;
                    }
                }else {
                    //调用注册方法
                    return register(json);
                }
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    private String saveToken(String user){
        //如果存在旧的用户token 则删除
        gameUserLoginInfoMapper.deleteGameUserLoginInfoByUser(user);

        GameUserLoginInfo gameUserLoginInfo = new GameUserLoginInfo();
        //生成token
        String random = Utils.random();
        gameUserLoginInfo.setUser(user);
        gameUserLoginInfo.setToken(random);
        gameUserLoginInfo.setTime(new Date());
        gameUserLoginInfoMapper.insertGameUserLoginInfo(gameUserLoginInfo);
        return random;
    }

    private JSONObject register(JSONObject json){
        //创建用户对象 从前端返回信息中获取账号密码 进行注册
        GameUser gameUser = new GameUser();
        gameUser.setUser(json.getString("user"));
        gameUser.setStatus("0");
        gameUser.setPassword(json.getString("pwd"));
        gameUser.setDate(new Date());
        //将用户信息插入数据库中
        gameUserMapper.insertGameUser(gameUser);
        //创建json对象
        System.out.println("这是生成的UserJson"+JSONObject.toJSONString(gameUser));
        JSONObject resultData = new JSONObject();

        //注册完毕后将注册的信息返回给前端
        String token = saveToken(String.valueOf(gameUser.getId()));
        resultData.put("action", "login");
        resultData.put("msg", "注册成功");
        resultData.put("code", "1");
        resultData.put("uid", gameUser.getId());
        resultData.put("tk", token);

        return resultData;
    }

}
